package com.steven.nio.ZeroCopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class CopyClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress(4332));
        FileChannel fileChannel = new FileInputStream("D:/openwrt.rar").getChannel();
        long size = fileChannel.size();
        long maxCopySize = 1024 * 1024 * 8;
        long position = 0;
        long l = System.currentTimeMillis();
        while (position < size) {
            maxCopySize = Math.min((size - position), maxCopySize);
            long transferTo = fileChannel.transferTo(position, maxCopySize, socketChannel);
            System.out.println("传输大小："+transferTo);
            position = position + maxCopySize;
        }
        long l1 = System.currentTimeMillis();
        long time = l1-l;
        System.out.println("花费时长："+time);
        fileChannel.close();

    }
}
